package com.ruoyi.framework.NoCodeConfig;

import com.ruoyi.framework.web.service.NoCodeUserDetailsServiceImpl;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

public class NoCodeCodeAuthenticationProvider implements AuthenticationProvider {
    private NoCodeUserDetailsServiceImpl noCodeUserDetailsService;



    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        NoCodeAuthenticationToken authenticationToken = (NoCodeAuthenticationToken) authentication;

        String username = (String) authenticationToken.getPrincipal();

//        checkSmsCode(mobile);

        UserDetails userDetails = noCodeUserDetailsService.loadUserByUsername(username);

        // 此时鉴权成功后，应当重新 new 一个拥有鉴权的 authenticationResult 返回
        NoCodeAuthenticationToken authenticationResult = new NoCodeAuthenticationToken(userDetails, userDetails.getAuthorities());

        authenticationResult.setDetails(authenticationToken.getDetails());

        return authenticationResult;
    }



    @Override
    public boolean supports(Class<?> authentication) {
        // 判断 authentication 是不是 NoCodeAuthenticationToken 的子类或子接口
        return NoCodeAuthenticationToken.class.isAssignableFrom(authentication);
    }

    public NoCodeUserDetailsServiceImpl getUserDetailsService() {
        return noCodeUserDetailsService;
    }

    public void setUserDetailsService(NoCodeUserDetailsServiceImpl userDetailsService) {
        this.noCodeUserDetailsService = userDetailsService;
    }
}
